import PureLayers from '../components/layers';
import * as CommonAction from 'actions';
import * as React from 'react';
import * as ReactRedux from 'react-redux';

const mapStateToProps = (state) => {
  return {
    layers: state.layers
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    onCloseClick: () => dispatch(CommonAction.popLayer()),
    clearLayer: () => dispatch(CommonAction.clearLayer())
  };
};

class Layers extends React.Component {
  componentWillReceiveProps(nextProps) {
    // 当 children（也就是第一层 Layer）被改变时，堆叠着的 Layer 应该被全部清除
    if (nextProps.children !== this.props.children) {
      this.props.clearLayer();
    }
  }

  render() {
    return <PureLayers {...this.props}>{this.props.children}</PureLayers>;
  }
}

export default ReactRedux.connect(mapStateToProps, mapDispatchToProps)(Layers);
